package com.xdqzy.baseline.common.config;

import com.xdqzy.baseline.common.constant.CommonConstants;
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : zhangdongdong
 * @version V1.0
 * @Title : swagger配置
 * @ClassName: com.xdqzy.baseline.common.config.SwaggerConfig.java
 * @Description:
 * @date :  2020-10-20 17:23
 */
@EnableKnife4j
@Import(BeanValidatorPluginsConfiguration.class)
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket api() {
        //可以添加多个header或参数
        ParameterBuilder aParameterBuilder = new ParameterBuilder();
        Parameter tokenParam = aParameterBuilder
                //参数类型支持header, cookie, body, query etc
                .parameterType("header")
                //http头授权header
                .name(CommonConstants.AUTHORIZATION)
                //默认值
                .defaultValue("token")
                .description("header中token字段,前后端分离需要添加")
                //指定参数值的类型
                .modelRef(new ModelRef("string"))
                //非必需，这里是全局配置，然而在登陆的时候是不用验证的
                .required(false).build();
        List<Parameter> aParameters = new ArrayList<>();
        aParameters.add(tokenParam);
        return new Docket(DocumentationType.SWAGGER_2).groupName("v1").select().apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.ant("/api/v1/**")).build().apiInfo(apiInfo1()).globalOperationParameters(aParameters);
    }


    private ApiInfo apiInfo1() {
        return new ApiInfoBuilder()
                .title("CTBC API 1.0")
                .termsOfServiceUrl("https://quss.coding.net/")
                .contact(new Contact("zhangdd", "https://github.com/winter0245", "winter0245@hotmail.com"))
                .version("v0.01")
                .build();
    }


}
